<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是STM32开发——Makefile文件内容分析的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV16-07-开发工具-03-Makefile文件">
<meta property="og:url" content="https://sumumm.github.io/post/4e3a140c.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是STM32开发——Makefile文件内容分析的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-06-04T14:06:38.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.008Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV16-STM32开发">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://sumumm.github.io/post/4e3a140c.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/4e3a140c.html","path":"post/4e3a140c.html","title":"LV16-07-开发工具-03-Makefile文件"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV16-07-开发工具-03-Makefile文件 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%AE%8C%E6%95%B4%E7%9A%84Makefile%E6%96%87%E4%BB%B6"><span class="nav-text">一、完整的Makefile文件</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%90%84%E9%83%A8%E5%88%86%E8%AF%B4%E6%98%8E"><span class="nav-text">二、各部分说明</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-target"><span class="nav-text">1. target</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-building-variables"><span class="nav-text">2. building variables</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-paths"><span class="nav-text">3. paths</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-source"><span class="nav-text">4. source</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-binaries"><span class="nav-text">5. binaries</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-CFLAGS"><span class="nav-text">6. CFLAGS</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-LDFLAGS"><span class="nav-text">7. LDFLAGS</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-%E7%9B%AE%E6%A0%87"><span class="nav-text">8. 目标</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-%E6%96%87%E4%BB%B6%E5%88%97%E8%A1%A8"><span class="nav-text">9. 文件列表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-%E7%BC%96%E8%AF%91%E9%93%BE%E6%8E%A5"><span class="nav-text">10. 编译链接</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-clean-up"><span class="nav-text">11. clean up</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-dependencies"><span class="nav-text">12. dependencies</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/4e3a140c.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV16-07-开发工具-03-Makefile文件 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV16-07-开发工具-03-Makefile文件
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-06-04 22:06:38" itemprop="dateCreated datePublished" datetime="2023-06-04T22:06:38+08:00">2023-06-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV16-STM32开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>6.1k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>22 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是STM32开发——Makefile文件内容分析的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-07-%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7-03-Makefile%E6%96%87%E4%BB%B6/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <ul><li>通用</li></ul><table><tr><td align="center">分类  </td><td align="center">网址</td><td align="center">说明</td></tr><tr><td align="center" rowspan="4">官方网站</td><td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td><td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td></tr><tr>                                            <td align="left"><a href="https://www.st.com/content/st_com/zh.html" target="_blank">https://www.st.com/content/st_com/zh.html</a></td><td align="left">ST官方网站，在这里我们可以找到STM32的相关文档</td></tr><tr>                                            <td align="left"><a href="https://www.stmcu.com.cn/" target="_blank">https://www.stmcu.com.cn/</a></td><td align="left">意法半导体ST中文官方网站，在这里我们可以找到STM32的相关中文参考文档</td></tr><tr>                                            <td align="left"><a href="http://elm-chan.org/fsw/ff/00index_e.html" target="_blank">http://elm-chan.org/fsw/ff/00index_e.html</a></td><td align="left">FatFs文件系统官网</td></tr><tr><td align="center" rowspan="3">教程书籍</td><td align="left"><a href="STM32开发相关资料/01ARM参考资料/ARM Cortex-M3权威指南(中文).pdf" target="_blank">《ARM Cortex-M3权威指南》</a></td><td alirn="left" rowspan="3">ARM公司专家Joseph Yiu（姚文祥）的力作，中文翻译是NXP的宋岩</td></tr><tr>                                            <td align="left"><a href="STM32开发相关资料/01ARM参考资料/ARM Cortex-M0权威指南(中文).pdf" target="_blank">《ARM Cortex-M0权威指南》</a></td></tr><tr>                                            <td align="left"><a href="" target="_blank">《ARM Cortex-M3与Cortex-M4权威指南》</a></td></tr><tr><td align="center" rowspan="4">开发论坛</td><td align="left"><a href="http://47.111.11.73/forum.php" target="_blank">http://47.111.11.73/forum.php</a></td><td align="left">开源电子网，正点原子的资料下载及问题讨论论坛</td></tr><tr>                                            <td align="left"><a href="https://www.firebbs.cn/forum.php" target="_blank">https://www.firebbs.cn/forum.php</a></td><td align="left">国内Kinetis开发板-野火/秉火（刘火良）主持的论坛，现也做STM32和i.MX RT</td></tr><tr>                                            <td align="left"><a href="https://www.amobbs.com/index.php" target="_blank">https://www.amobbs.com/index.php</a></td><td align="left">阿莫（莫进明）主持的论坛，号称国内最早最火的电子论坛，以交流Atmel AVR系列单片机起家，现已拓展到嵌入式全平台，其STM32系列帖子有70W+。</td></tr><tr>                                            <td align="left"><a href="http://download.100ask.net/index.html" target="_blank">http://download.100ask.net/index.html</a></td><td align="left">韦东山嵌入式资料中心，有些STM32和linux的相关资料也可以来这里找。</td></tr><tr><td align="center" rowspan="3">博客参考</td><td align="left"><a href="http://www.openedv.com/" target="_blank">http://www.openedv.com/</a></td><td align="left">开源网-原子哥个人博客</td></tr><tr>                                            <td align="left"><a href="http://blog.chinaaet.com/jihceng0622" target="_blank">http://blog.chinaaet.com/jihceng0622</a></td><td align="left">博主是原Freescale现NXP的现场应用工程师</td></tr><tr>                                            <td align="left"><a href="https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/cortex-m-resources" target="_blank">cortex-m-resources</a></td><td align="left">这其实并不算是一个博客，这是ARM公司专家Joseph Yiu收集整理的所有对开发者有用的官方Cortex-M资料链接（也包含极少数外部资源链接）</td></tr></table><ul><li>STM32</li></ul><table>    <tr><td align="center" rowspan="2">STM32</td><td align="left"><a href="https://doc.embedfire.com/mcu/stm32/f103/hal_general/zh/latest/index.html" target="_blank">STM32 HAL库开发实战指南——基于F103系列开发板</a></td><td align="left">野火STM32开发教程在线文档</td></tr><tr>                                            <td align="left"><a href="https://doc.embedfire.com/mcu/stm32/f103badao/std/zh/latest/index.html" target="_blank">STM32库开发实战指南——基于野火霸道开发板</a></td><td align="left">野火STM32开发教程在线文档</td></tr></table><ul><li>SD卡</li></ul><table>    <tr><td align="left"><a href="https://www.sdcard.org/" target="_blank">SD Association</a></td><td align="left">提供了SD存储卡和SDIO卡系统规范</td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr><td align="left"><a href="https://www.st.com/resource/en/datasheet/stm32f103ze.pdf" target="_blank">STM32F103xx英文数据手册</a></td><td align="left">STM32F103xC/D/E系列的英文数据手册</td></tr>    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/709978" target="_blank">STM32F103xx中文数据手册</a></td><td align="left">STM32F103xC/D/E系列的中文数据手册</td></tr>    <tr><td align="left"><a href="https://www.st.com/resource/en/reference_manual/rm0008-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf" target="_blank">STM32F10xxx英文参考手册（RM0008）</a></td><td align="left">STM32F10xxx系列的英文参考手册</td></tr>    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/710001" target="_blank">STM32F10xxx中文参考手册（RM0008）</a></td><td align="left">STM32F10xxx系列的中文参考手册</td></tr>    <tr><td align="left"><a href="https://developer.arm.com/documentation/100165/0201/?lang=en" target="_blank">Arm Cortex-M3 处理器技术参考手册-英文版</a></td><td align="left">Cortex-M3技术参考手册-英文版</td></tr>    <tr><td align="left"><a href="https://www.st.com/resource/en/programming_manual/pm0056-stm32f10xxx20xxx21xxxl1xxxx-cortexm3-programming-manual-stmicroelectronics.pdf" target="_blank">STM32F10xxx Cortex-M3编程手册-英文版(PM0056)</a></td><td align="left">STM32F10xxx/20xxx/21xxx/L1xxxx系列Cortex-M3编程手册-英文版</td></tr>    <tr><td align="left"><a href="https://www.sdcard.org/downloads/pls/" target="_blank">SD卡相关资料——最新版本</a></td><td align="left">有关SD卡的一些资料可以从这里下载</td></tr>    <tr><td align="left"><a href="https://www.sdcard.org/downloads/pls/archives/" target="_blank">SD卡相关资料——历史版本</a></td><td align="left">有关SD卡的一些历史版本资料可以从这里下载，比如后边看的SD卡2.0协议</td></tr>    <tr><td align="left"><a href="./" target="_blank">SD 2.0 协议标准完整版</a></td><td align="left">这是一篇关于SD卡2.0协议的中文文档，还是比较有参考价值的，可以一看</td></tr></table>
              </div>
            </details> 

<p>这篇笔记主要是STM32CubeMX软件生成的Makefile文件的学习笔记。这篇笔记按照顺序从上到下分析Makefile文件。</p>
<h1 id="一、完整的Makefile文件"><a href="#一、完整的Makefile文件" class="headerlink" title="一、完整的Makefile文件"></a><font size=3>一、完整的Makefile文件</font></h1><details class="folding-tag" blue><summary> 点击查看完整的Makefile文件 </summary>
              <div class='content'>
              <figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">##########################################################################################################################</span></span><br><span class="line"><span class="comment"># File automatically-generated by tool: [projectgenerator] version: [3.19.2] date: [Thu Apr 27 16:55:10 CST 2023] </span></span><br><span class="line"><span class="comment">##########################################################################################################################</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># ------------------------------------------------</span></span><br><span class="line"><span class="comment"># Generic Makefile (based on gcc)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># ChangeLog :</span></span><br><span class="line"><span class="comment">#	2017-02-10 - Several enhancements + project update mode</span></span><br><span class="line"><span class="comment">#   2015-07-22 - first version</span></span><br><span class="line"><span class="comment"># ------------------------------------------------</span></span><br><span class="line"></span><br><span class="line"><span class="comment">######################################</span></span><br><span class="line"><span class="comment"># target</span></span><br><span class="line"><span class="comment">######################################</span></span><br><span class="line">TARGET = makefile_project</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">######################################</span></span><br><span class="line"><span class="comment"># building variables</span></span><br><span class="line"><span class="comment">######################################</span></span><br><span class="line"><span class="comment"># debug build?</span></span><br><span class="line">DEBUG = 1</span><br><span class="line"><span class="comment"># optimization</span></span><br><span class="line">OPT = -Og</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># paths</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># Build path</span></span><br><span class="line">BUILD_DIR = build</span><br><span class="line"></span><br><span class="line"><span class="comment">######################################</span></span><br><span class="line"><span class="comment"># source</span></span><br><span class="line"><span class="comment">######################################</span></span><br><span class="line"><span class="comment"># C sources</span></span><br><span class="line">C_SOURCES =  \</span><br><span class="line">Core/Src/main.c \</span><br><span class="line">Core/Src/stm32f1xx_it.c \</span><br><span class="line">Core/Src/stm32f1xx_hal_msp.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c \</span><br><span class="line">Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c \</span><br><span class="line">Core/Src/system_stm32f1xx.c \</span><br><span class="line">Core/Src/gpio.c</span><br><span class="line"></span><br><span class="line"><span class="comment"># ASM sources</span></span><br><span class="line">ASM_SOURCES =  \</span><br><span class="line">startup_stm32f103xe.s</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># binaries</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line">PREFIX = arm-none-eabi-</span><br><span class="line"><span class="comment"># The gcc compiler bin path can be either defined in make command via GCC_PATH variable (&gt; make GCC_PATH=xxx)</span></span><br><span class="line"><span class="comment"># either it can be added to the PATH environment variable.</span></span><br><span class="line"><span class="keyword">ifdef</span> GCC_PATH</span><br><span class="line">CC = <span class="variable">$(GCC_PATH)</span>/<span class="variable">$(PREFIX)</span>gcc</span><br><span class="line">AS = <span class="variable">$(GCC_PATH)</span>/<span class="variable">$(PREFIX)</span>gcc -x assembler-with-cpp</span><br><span class="line">CP = <span class="variable">$(GCC_PATH)</span>/<span class="variable">$(PREFIX)</span>objcopy</span><br><span class="line">SZ = <span class="variable">$(GCC_PATH)</span>/<span class="variable">$(PREFIX)</span>size</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">CC = <span class="variable">$(PREFIX)</span>gcc</span><br><span class="line">AS = <span class="variable">$(PREFIX)</span>gcc -x assembler-with-cpp</span><br><span class="line">CP = <span class="variable">$(PREFIX)</span>objcopy</span><br><span class="line">SZ = <span class="variable">$(PREFIX)</span>size</span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line">HEX = <span class="variable">$(CP)</span> -O ihex</span><br><span class="line">BIN = <span class="variable">$(CP)</span> -O binary -S</span><br><span class="line"> </span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># CFLAGS</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># cpu</span></span><br><span class="line">CPU = -mcpu=cortex-m3</span><br><span class="line"></span><br><span class="line"><span class="comment"># fpu</span></span><br><span class="line"><span class="comment"># NONE for Cortex-M0/M0+/M3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># float-abi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># mcu</span></span><br><span class="line">MCU = <span class="variable">$(CPU)</span> -mthumb <span class="variable">$(FPU)</span> $(FLOAT-ABI)</span><br><span class="line"></span><br><span class="line"><span class="comment"># macros for gcc</span></span><br><span class="line"><span class="comment"># AS defines</span></span><br><span class="line">AS_DEFS = </span><br><span class="line"></span><br><span class="line"><span class="comment"># C defines</span></span><br><span class="line">C_DEFS =  \</span><br><span class="line">-DUSE_HAL_DRIVER \</span><br><span class="line">-DSTM32F103xE</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># AS includes</span></span><br><span class="line">AS_INCLUDES = </span><br><span class="line"></span><br><span class="line"><span class="comment"># C includes</span></span><br><span class="line">C_INCLUDES =  \</span><br><span class="line">-ICore/Inc \</span><br><span class="line">-IDrivers/STM32F1xx_HAL_Driver/Inc \</span><br><span class="line">-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \</span><br><span class="line">-IDrivers/CMSIS/Device/ST/STM32F1xx/Include \</span><br><span class="line">-IDrivers/CMSIS/Include</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># compile gcc flags</span></span><br><span class="line">ASFLAGS = <span class="variable">$(MCU)</span> <span class="variable">$(AS_DEFS)</span> <span class="variable">$(AS_INCLUDES)</span> <span class="variable">$(OPT)</span> -Wall -fdata-sections -ffunction-sections</span><br><span class="line"></span><br><span class="line">CFLAGS += <span class="variable">$(MCU)</span> <span class="variable">$(C_DEFS)</span> <span class="variable">$(C_INCLUDES)</span> <span class="variable">$(OPT)</span> -Wall -fdata-sections -ffunction-sections</span><br><span class="line"></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(DEBUG)</span>, 1)</span><br><span class="line">CFLAGS += -g -gdwarf-2</span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># Generate dependency information</span></span><br><span class="line">CFLAGS += -MMD -MP -MF<span class="string">&quot;$(@:%.o=%.d)&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># LDFLAGS</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># link script</span></span><br><span class="line">LDSCRIPT = STM32F103ZETx_FLASH.ld</span><br><span class="line"></span><br><span class="line"><span class="comment"># libraries</span></span><br><span class="line">LIBS = -lc -lm -lnosys </span><br><span class="line">LIBDIR = </span><br><span class="line">LDFLAGS = <span class="variable">$(MCU)</span> -specs=nano.specs -T<span class="variable">$(LDSCRIPT)</span> <span class="variable">$(LIBDIR)</span> <span class="variable">$(LIBS)</span> -Wl,-Map=<span class="variable">$(BUILD_DIR)</span>/<span class="variable">$(TARGET)</span>.map,--cref -Wl,--gc-sections</span><br><span class="line"></span><br><span class="line"><span class="comment"># default action: build all</span></span><br><span class="line"><span class="section">all: <span class="variable">$(BUILD_DIR)</span>/<span class="variable">$(TARGET)</span>.elf <span class="variable">$(BUILD_DIR)</span>/<span class="variable">$(TARGET)</span>.hex <span class="variable">$(BUILD_DIR)</span>/<span class="variable">$(TARGET)</span>.bin</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># build the application</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># list of objects</span></span><br><span class="line">OBJECTS = <span class="variable">$(<span class="built_in">addprefix</span> <span class="variable">$(BUILD_DIR)</span>/,$(<span class="built_in">notdir</span> $(C_SOURCES:.c=.o)</span>))</span><br><span class="line"><span class="keyword">vpath</span> %.c <span class="variable">$(<span class="built_in">sort</span> $(<span class="built_in">dir</span> <span class="variable">$(C_SOURCES)</span>)</span>)</span><br><span class="line"><span class="comment"># list of ASM program objects</span></span><br><span class="line">OBJECTS += <span class="variable">$(<span class="built_in">addprefix</span> <span class="variable">$(BUILD_DIR)</span>/,$(<span class="built_in">notdir</span> $(ASM_SOURCES:.s=.o)</span>))</span><br><span class="line"><span class="keyword">vpath</span> %.s <span class="variable">$(<span class="built_in">sort</span> $(<span class="built_in">dir</span> <span class="variable">$(ASM_SOURCES)</span>)</span>)</span><br><span class="line"></span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>/%.o: %.c Makefile | <span class="variable">$(BUILD_DIR)</span> </span><br><span class="line">	<span class="variable">$(CC)</span> -c <span class="variable">$(CFLAGS)</span> -Wa,-a,-ad,-alms=<span class="variable">$(BUILD_DIR)</span>/<span class="variable">$(<span class="built_in">notdir</span> $(&lt;:.c=.lst)</span>) <span class="variable">$&lt;</span> -o <span class="variable">$@</span></span><br><span class="line"></span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>/%.o: %.s Makefile | <span class="variable">$(BUILD_DIR)</span></span><br><span class="line">	<span class="variable">$(AS)</span> -c <span class="variable">$(CFLAGS)</span> <span class="variable">$&lt;</span> -o <span class="variable">$@</span></span><br><span class="line"></span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>/<span class="variable">$(TARGET)</span>.elf: <span class="variable">$(OBJECTS)</span> Makefile</span><br><span class="line">	<span class="variable">$(CC)</span> <span class="variable">$(OBJECTS)</span> <span class="variable">$(LDFLAGS)</span> -o <span class="variable">$@</span></span><br><span class="line">	<span class="variable">$(SZ)</span> <span class="variable">$@</span></span><br><span class="line"></span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>/%.hex: <span class="variable">$(BUILD_DIR)</span>/%.elf | <span class="variable">$(BUILD_DIR)</span></span><br><span class="line">	<span class="variable">$(HEX)</span> <span class="variable">$&lt;</span> <span class="variable">$@</span></span><br><span class="line">	</span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>/%.bin: <span class="variable">$(BUILD_DIR)</span>/%.elf | <span class="variable">$(BUILD_DIR)</span></span><br><span class="line">	<span class="variable">$(BIN)</span> <span class="variable">$&lt;</span> <span class="variable">$@</span>	</span><br><span class="line">	</span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>:</span><br><span class="line">	mkdir <span class="variable">$@</span>		</span><br><span class="line"></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># clean up</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	-rm -fR <span class="variable">$(BUILD_DIR)</span></span><br><span class="line">  </span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># dependencies</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="keyword">-include</span> <span class="variable">$(<span class="built_in">wildcard</span> <span class="variable">$(BUILD_DIR)</span>/*.d)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># *** EOF ***</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h1 id="二、各部分说明"><a href="#二、各部分说明" class="headerlink" title="二、各部分说明"></a><font size=3>二、各部分说明</font></h1><h2 id="1-target"><a href="#1-target" class="headerlink" title="1. target"></a><font size=3>1. target</font></h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">######################################</span></span><br><span class="line"><span class="comment"># target</span></span><br><span class="line"><span class="comment">######################################</span></span><br><span class="line">TARGET = makefile_project</span><br></pre></td></tr></table></figure>

<p>设置目标文件名，实际上就是做了个变量，并且把Makefile这个作为参数传入。平时当成工程名即可，如果要换工程，就自己随便重新取个名字。</p>
<h2 id="2-building-variables"><a href="#2-building-variables" class="headerlink" title="2. building variables"></a><font size=3>2. building variables</font></h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">######################################</span></span><br><span class="line"><span class="comment"># building variables</span></span><br><span class="line"><span class="comment">######################################</span></span><br><span class="line"><span class="comment"># debug build?</span></span><br><span class="line">DEBUG = 1</span><br><span class="line"><span class="comment"># optimization</span></span><br><span class="line">OPT = -Og</span><br></pre></td></tr></table></figure>

<p>（1）DEBUG：是否进行调试编译，这里表示是，release版关闭调试时将1改成其它数即可，具体原因后面会提到。</p>
<p>（2）优化等级，这里使用Og的目的是因为开启了调试，所以优化时需要产生合理的优化而不和调试选项冲突。如果是release版本，可以使用 -Os （提高速度并优化体积）或 -O3 （大幅提高速度但会增大体积）等命令，关于这部分的介绍可以参考<a target="_blank" rel="noopener" href="https://www.zhihu.com/question/27090458/answer/137944410">知乎回答</a>。</p>
<h2 id="3-paths"><a href="#3-paths" class="headerlink" title="3. paths"></a><font size=3>3. paths</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#######################################</span><br><span class="line"><span class="meta"># paths</span></span><br><span class="line">#######################################</span><br><span class="line"># Build path</span><br><span class="line">BUILD_DIR = build</span><br></pre></td></tr></table></figure>

<p>编译目标地址，这也是前面提到为什么我会生成一个build文件夹，实际上就是这个变量进行的命名，如果有需要，可以把它修改成任何名字，然后编译出来的所有文件就都会放在这个新命名的文件夹里面了。不过一般不建议修改，因为大家通用的都是build。</p>
<h2 id="4-source"><a href="#4-source" class="headerlink" title="4. source"></a><font size=3>4. source</font></h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">######################################</span></span><br><span class="line"><span class="comment"># source</span></span><br><span class="line"><span class="comment">######################################</span></span><br><span class="line"><span class="comment"># C sources</span></span><br><span class="line">C_SOURCES =  \</span><br><span class="line">Core/Src/main.c \</span><br><span class="line"><span class="comment">#中间的省略......</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># ASM sources</span></span><br><span class="line">ASM_SOURCES =  \</span><br><span class="line">startup_stm32f103xe.s</span><br></pre></td></tr></table></figure>

<p>（1）C_SOURCES：这里放的就是所有用到的 c 文件。如果我们需要自己写个什么库自己写了什么新的 c 文件，而且最后会被编译到程序里面的话，就都需要自己在这里添加一行 c 文件地址。这些地址可以是相对路径，相对的就是makefile文件的地址，也可以设置为绝对路径，例如这里面所有Drivers的文件，如果我们没有配置为复制到工程里面，那么这些文件就会被写为绝对路径并导航到STM32CubeMX的sdk存储地址里面。</p>
<p>（2）ASM_SOURCES：所有汇编文件的地址，添加方法可以继续参考前面C文件的添加方法，也是绝对地址和相对地址添加即可。而这里的start文件，一般不需要用户自己写，这些厂商在开发好芯片后都至少会提供一个startup的c或s文件的，我们需要做的就只是把他添加进来即可（有的可能不是汇编s文件，如果是c文件，正常添加到前面C文件的路径下面即可，这种情况下汇编这里就不需要添加startup了，不过此外自己写的其他汇编文件倒是仍然可以正常这样写进去）。</p>
<p><strong>【注意】</strong>当我们采用此种方式生成工程时，若是添加自己的源文件，更新工程时我们自己添加的会保留，不会被清空，可以放心添加。</p>
<h2 id="5-binaries"><a href="#5-binaries" class="headerlink" title="5. binaries"></a><font size=3>5. binaries</font></h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># binaries</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line">PREFIX = arm-none-eabi-</span><br><span class="line"><span class="comment"># The gcc compiler bin path can be either defined in make command via GCC_PATH variable (&gt; make GCC_PATH=xxx)</span></span><br><span class="line"><span class="comment"># either it can be added to the PATH environment variable.</span></span><br><span class="line"><span class="keyword">ifdef</span> GCC_PATH</span><br><span class="line">CC = <span class="variable">$(GCC_PATH)</span>/<span class="variable">$(PREFIX)</span>gcc</span><br><span class="line">AS = <span class="variable">$(GCC_PATH)</span>/<span class="variable">$(PREFIX)</span>gcc -x assembler-with-cpp</span><br><span class="line">CP = <span class="variable">$(GCC_PATH)</span>/<span class="variable">$(PREFIX)</span>objcopy</span><br><span class="line">SZ = <span class="variable">$(GCC_PATH)</span>/<span class="variable">$(PREFIX)</span>size</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">CC = <span class="variable">$(PREFIX)</span>gcc</span><br><span class="line">AS = <span class="variable">$(PREFIX)</span>gcc -x assembler-with-cpp</span><br><span class="line">CP = <span class="variable">$(PREFIX)</span>objcopy</span><br><span class="line">SZ = <span class="variable">$(PREFIX)</span>size</span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line">HEX = <span class="variable">$(CP)</span> -O ihex</span><br><span class="line">BIN = <span class="variable">$(CP)</span> -O binary -S</span><br></pre></td></tr></table></figure>

<p>（1）PREFIX ：工具链起始名，就是使用的开源工具链，这里变量的内容是arm-none-eabi-，其实这个是一个gcc工具链的名字，这个工具链全程一般叫arm-none-eabi-gcc，arm表示架构平台，none表示无系统，eabi表示嵌入式应用二进制接口，gcc表示其遵守的GNU GCC的设计规范。</p>
<p>（2）GCC_PATH ：这个就是我们gcc工具链的地址。这就涉及到一个问题，也就是环境变量，如果我们将编译工具链的工具目录地址添加到环境变量中，那么我们就可以在命令行中直接通过工具的名称调用这个工具，否则我们就需要输入绝对路径来调用工具。</p>
<p>比如，假设arm-none-eabi-gcc存储在D盘根目录下，那么很可能需要在环境变量中添加 D:\arm-none-eabi-gcc\bin\ 文件夹地址，此时我们就可以在命令行中直接使用 arm-none-eabi-gcc 来运行 D:\arm-none-eabi-gcc\bin\arm-none-eabi-gcc.exe 程序，否则，我们就需要输入完整的绝对路径来调用该程序。</p>
<p>而GCC_PATH就是用来帮助我们添加这个绝对路径的，如果没有添加环境变量，那么就可以在makefile前面定义GCC_PATH并添加上地址，如D:\arm-none-eabi-gcc\bin，那么之后我们会运行ifdef中的程序（如果没定义就直接使用后面else的程序），makefile所有变量的本质其实就是字符替换，此时例如CC，就会被编译为D:\arm-none-eabi-gcc\bin\arm-none-eabi-gcc.exe，其余同理。当然，如果我们不想在makefile中添加这个变量，也可以选择在输入make指令的时候添加 GCC_PATH&#x3D;D:\arm-none-eabi-gcc\bin 。</p>
<p>（3）CC ：直接使用gcc，其实代表了其默认参数编译C语言。</p>
<p>（4）AS ：代表了汇编器，后边的两个参数没看懂，暂时不关心。</p>
<p>（5）CP ：表示objcopy，这是个复制文件内容的工具。</p>
<p>（6）SZ ：是一个记录生成程序内部各分段的大小的工具，这个在编译后查看编译出的文件大小进行项目优化的时候会比较有用，makefile中也用了这个工具，在编译到最后会输出这样的内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">arm-none-eabi-size build/stm32f103-prj.elf</span><br><span class="line">   text    data     bss     dec     hex filename</span><br><span class="line">   9484     112    2552   12148    2f74 build/stm32f103-prj.elf</span><br></pre></td></tr></table></figure>

<p>值得注意的是这个size工具只能对elf文件进行解析，而hex和bin文件中是不包含代码段数据段这些东西的。这里的大概意思是我的代码段有9484字节，数据段有112字节，bss段有2552字节，总共加起来有12148字节。</p>
<p>（7）HEX 和 BIN ：这里其实就是使用CP工具，生成十六进制文件和二进制文件，他们都是可以被烧录到stm32中的代码文件格式，只是包含的信息有所不同。</p>
<h2 id="6-CFLAGS"><a href="#6-CFLAGS" class="headerlink" title="6. CFLAGS"></a><font size=3>6. CFLAGS</font></h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># CFLAGS</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># cpu</span></span><br><span class="line">CPU = -mcpu=cortex-m3</span><br><span class="line"></span><br><span class="line"><span class="comment"># fpu</span></span><br><span class="line"><span class="comment"># NONE for Cortex-M0/M0+/M3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># float-abi</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># mcu</span></span><br><span class="line">MCU = <span class="variable">$(CPU)</span> -mthumb <span class="variable">$(FPU)</span> $(FLOAT-ABI)</span><br><span class="line"></span><br><span class="line"><span class="comment"># macros for gcc</span></span><br><span class="line"><span class="comment"># AS defines</span></span><br><span class="line">AS_DEFS = </span><br><span class="line"></span><br><span class="line"><span class="comment"># C defines</span></span><br><span class="line">C_DEFS =  \</span><br><span class="line">-DUSE_HAL_DRIVER \</span><br><span class="line">-DSTM32F103xE</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># AS includes</span></span><br><span class="line">AS_INCLUDES = </span><br><span class="line"></span><br><span class="line"><span class="comment"># C includes</span></span><br><span class="line">C_INCLUDES =  \</span><br><span class="line">-ICore/Inc \</span><br><span class="line">-IDrivers/STM32F1xx_HAL_Driver/Inc \</span><br><span class="line">-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \</span><br><span class="line">-IDrivers/CMSIS/Device/ST/STM32F1xx/Include \</span><br><span class="line">-IDrivers/CMSIS/Include</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># compile gcc flags</span></span><br><span class="line">ASFLAGS = <span class="variable">$(MCU)</span> <span class="variable">$(AS_DEFS)</span> <span class="variable">$(AS_INCLUDES)</span> <span class="variable">$(OPT)</span> -Wall -fdata-sections -ffunction-sections</span><br><span class="line"></span><br><span class="line">CFLAGS += <span class="variable">$(MCU)</span> <span class="variable">$(C_DEFS)</span> <span class="variable">$(C_INCLUDES)</span> <span class="variable">$(OPT)</span> -Wall -fdata-sections -ffunction-sections</span><br><span class="line"></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(DEBUG)</span>, 1)</span><br><span class="line">CFLAGS += -g -gdwarf-2</span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># Generate dependency information</span></span><br><span class="line">CFLAGS += -MMD -MP -MF<span class="string">&quot;$(@:%.o=%.d)&quot;</span></span><br></pre></td></tr></table></figure>

<p>（1）CPU ：选择cpu的架构，因为我使用的是stm32f1的芯片，故这里的架构是cortex-m3，如果是其它架构需要进行修改。</p>
<p>（2）FPU ：选择fpu的架构，Cortex-M0&#x2F;M0+&#x2F;M3没有，那么就直接注释掉就可以了，关闭后无法使用fpu对浮点数计算进行加速。</p>
<p>（3）float-abi ：硬浮点或软浮点，如果有FPU就可以开启硬浮点，这里没有就直接注释了。</p>
<p>（4）MCU ：此处代表了所有针对MCU硬件的配置，这个 -mthumb 代表的是T32指令集，如果使用了另外一种指令集的架构就需要换这里的参数。</p>
<p>（5）AS_DEFS ：汇编中使用的宏定义，这里没有，后边就是空啦。</p>
<p>（6）C_DEFS ：而C文件宏定义，真正的宏需要去掉每个宏定义开头的-D，例如此处的-DUSE_HAL_DRIVER ，实际上是宏定义了USE_HAL_DRIVER，这些宏定义会影响到c文件里面相关宏的内容，例如这个HAL，其实就代表了HAL库，那么C文件里面引用的东西就会选择HAL库而非LL库的内容。如果有需要的话，也可以自己添加一些宏定义在c文件中作为开关，然后在这里添加东西表示打开。</p>
<p>（7）AS_INCLUDES ：汇编引用的头文件所在的目录其中因为汇编没有引用外部文件，所以什么也没有，如果有需要自己添加。</p>
<p>（8）C_INCLUDES ：对于C来说，如果在自己的代码#include中<strong>使用了相对地址或绝对地址，则可以不添加</strong>，如果是只写了引用文件名或非本文件的文件名，则需要添加，以辅助编译器找到对应文件，编译器会主动从上述路径作为相对路径寻找include的文件。这里 -I(大写的i) 也全部是前缀。<strong>【注意】</strong>当我们采用此种方式生成工程时，若是添加自己的头文件目录，更新工程时我们自己添加的会保留，不会被清空，可以放心添加。</p>
<p>例如我们需要在Core\Src\main.c中引用Core\Test\test.h，那么我们需要在main.c中写#include “test.h”并在C_INCLUDES中添加-ICore&#x2F;Test，而如果我们不在C_INCLUDES中进行添加，那么main.c中就必须写相对或绝对地址，例如#include “..&#x2F;Test&#x2F;test.h”，这样才能保证我们的编译器能够找准位置。</p>
<p>（9）ASFLAGS、CFLAGS ：汇编和C源文件的编译参数，-Wall的功能主要是开启一些警告检测，如果我们的代码出现了不规范的地方，那么通过这个参数，编译器就会检查到这些地方并告知我们。-fdata-sections表示对程序中每个数据创建一个section，而-ffunction-sections则是针对函数创建section，GCC的链接操作是以section作为最小的处理单元，只要一个section中的某个符号被引用，该section就会被加到可执行程序中。</p>
<p>（10）关于这个部分的内容是针对CFLAGS：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(DEBUG)</span>, 1)</span><br><span class="line">CFLAGS += -g -gdwarf-2</span><br><span class="line"><span class="keyword">endif</span></span><br></pre></td></tr></table></figure>

<p>如果DEBUG&#x3D;1，就执行，如果不是1，就不执行。很显然，这段话就是检测目前是否是调试模式，而注意这里CFLAGS前面其实已经出现过，而这里再次出现时使用的就不再是等号&#x3D;了，而是+&#x3D;符号，这个符号在makefile中表示在已有变量后面增加新的符号。而增加的其实就是两个新的参数，我们应该不难猜测这两个参数就是针对的调试生成的。</p>
<p>-g表示的意思是生成调试信息，如果不生成的话我们在使用gdb调试的时候就很难做到代码定位，当然如果不使用gdb，还可以使用-gformat然后加上你想要的格式。而-gdwarf-2的意思是生成DWARF version2 的格式的调试信息，常用于IRIXX6上的DBX调试器。GCC会使用DWARF version3的一些特性。</p>
<p>（11）这一小部分还是针对CFLAGS：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Generate dependency information</span></span><br><span class="line">CFLAGS += -MMD -MP -MF<span class="string">&quot;$(@:%.o=%.d)&quot;</span></span><br></pre></td></tr></table></figure>

<p>这一串东西的主要功能是生成关联信息，关联信息会对链接产生影响，其中-MMD表示会把关联信息输出到.d文件中，-MF是指定用于存放关联信息的文件，-MP生成的依赖文件里面，依赖规则中的所有 .h 依赖项都会在该文件中生成一个伪目标，其不依赖任何其他依赖项。该伪规则将避免删除了对应的头文件而没有更新 “Makefile” 去匹配新的依赖关系而导致 make 出错的情况出现。后边双引号里边的，说实话，没看懂，问题不大，后边看懂了再补充。</p>
<h2 id="7-LDFLAGS"><a href="#7-LDFLAGS" class="headerlink" title="7. LDFLAGS"></a><font size=3>7. LDFLAGS</font></h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># LDFLAGS</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># link script</span></span><br><span class="line">LDSCRIPT = STM32F103ZETx_FLASH.ld</span><br><span class="line"></span><br><span class="line"><span class="comment"># libraries</span></span><br><span class="line">LIBS = -lc -lm -lnosys </span><br><span class="line">LIBDIR = </span><br><span class="line">LDFLAGS = <span class="variable">$(MCU)</span> -specs=nano.specs -T<span class="variable">$(LDSCRIPT)</span> <span class="variable">$(LIBDIR)</span> <span class="variable">$(LIBS)</span> -Wl,-Map=<span class="variable">$(BUILD_DIR)</span>/<span class="variable">$(TARGET)</span>.map,--cref -Wl,--gc-sections</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>（1）LDSCRIPT ：链接脚本，里面包括了链接时各程序的组织架构，以及包括堆栈地址大小等信息，这个东西是CubeMX生成的，所以里面其实很多东西我们在CubeMX中都可以修改。这里的内容不只是修改芯片，只要修改了了中断向量、堆栈大小等信息，均需要修改此部分，它其实就代表了程序在芯片的ROM中是怎么存储的。</p>
<p>（2）LIBS ：代表的链接库，其实就是C语言的标准库，比如说这个-lm就代表了链接了标准数学库，不过具体这几个库链接的都是哪几个，具体可能要参考GCC的手册。</p>
<p>（3）LIBDIR ：如果使用的是非标准库需要从外面引用的话，就需要在这里添加东西了，添加的方法和前面添加c和汇编库的方法类似。</p>
<p>（4）LDFLAGS ：这个就是链接的参数。</p>
<p>-specs&#x3D;nano.specs替换精简c库以缩小代码大小。</p>
<p>-T使用脚本文件xxx作为链接器脚本，此脚本替换ld的默认链接脚本（而非加进去），因此命令文件必须给出所有输出文件所需的东西，这里的链接脚本我们在前面已经说过了。</p>
<p>-Wl如果链接器不是直接被使用的，而是被编译器使用（如gcc），则所有的链接命令前面必须加上-Wl参数（或其他特殊编译器对应的参数）。</p>
<p>-MAP打印一个链接表到map文件中。</p>
<p>–cref输出一个交叉引用表。如果一个链接映射文件正在被生成，交叉参照表将会被输出到映射文件中。否则，将会被输出到标准输出。</p>
<p>–gc-sections允许对未使用的段使用垃圾收集。在不支持这个选项的目标上将被自动忽略。默认的行为可用’–no-gc-sections’恢复。</p>
<h2 id="8-目标"><a href="#8-目标" class="headerlink" title="8. 目标"></a><font size=3>8. 目标</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"># default action: build all</span></span><br><span class="line">all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin</span><br></pre></td></tr></table></figure>

<p>默认执行全部编译，生成elf、hex和bin文件。</p>
<h2 id="9-文件列表"><a href="#9-文件列表" class="headerlink" title="9. 文件列表"></a><font size=3>9. 文件列表</font></h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># list of objects</span></span><br><span class="line">OBJECTS = <span class="variable">$(<span class="built_in">addprefix</span> <span class="variable">$(BUILD_DIR)</span>/,$(<span class="built_in">notdir</span> $(C_SOURCES:.c=.o)</span>))</span><br><span class="line"><span class="keyword">vpath</span> %.c <span class="variable">$(<span class="built_in">sort</span> $(<span class="built_in">dir</span> <span class="variable">$(C_SOURCES)</span>)</span>)</span><br><span class="line"><span class="comment"># list of ASM program objects</span></span><br><span class="line">OBJECTS += <span class="variable">$(<span class="built_in">addprefix</span> <span class="variable">$(BUILD_DIR)</span>/,$(<span class="built_in">notdir</span> $(ASM_SOURCES:.s=.o)</span>))</span><br><span class="line"><span class="keyword">vpath</span> %.s <span class="variable">$(<span class="built_in">sort</span> $(<span class="built_in">dir</span> <span class="variable">$(ASM_SOURCES)</span>)</span>)</span><br></pre></td></tr></table></figure>

<p>这里主要是将所有的源文件替换成.o，方便后边生成中间文件。vpath用于设置源文件的路径，这样makefile就知道去哪里找源文件啦。</p>
<h2 id="10-编译链接"><a href="#10-编译链接" class="headerlink" title="10. 编译链接"></a><font size=3>10. 编译链接</font></h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$(BUILD_DIR)</span>/%.o: %.c Makefile | <span class="variable">$(BUILD_DIR)</span> </span><br><span class="line">	<span class="variable">$(CC)</span> -c <span class="variable">$(CFLAGS)</span> -Wa,-a,-ad,-alms=<span class="variable">$(BUILD_DIR)</span>/<span class="variable">$(<span class="built_in">notdir</span> $(&lt;:.c=.lst)</span>) <span class="variable">$&lt;</span> -o <span class="variable">$@</span></span><br><span class="line"></span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>/%.o: %.s Makefile | <span class="variable">$(BUILD_DIR)</span></span><br><span class="line">	<span class="variable">$(AS)</span> -c <span class="variable">$(CFLAGS)</span> <span class="variable">$&lt;</span> -o <span class="variable">$@</span></span><br><span class="line"></span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>/<span class="variable">$(TARGET)</span>.elf: <span class="variable">$(OBJECTS)</span> Makefile</span><br><span class="line">	<span class="variable">$(CC)</span> <span class="variable">$(OBJECTS)</span> <span class="variable">$(LDFLAGS)</span> -o <span class="variable">$@</span></span><br><span class="line">	<span class="variable">$(SZ)</span> <span class="variable">$@</span></span><br><span class="line"></span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>/%.hex: <span class="variable">$(BUILD_DIR)</span>/%.elf | <span class="variable">$(BUILD_DIR)</span></span><br><span class="line">	<span class="variable">$(HEX)</span> <span class="variable">$&lt;</span> <span class="variable">$@</span></span><br><span class="line">	</span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>/%.bin: <span class="variable">$(BUILD_DIR)</span>/%.elf | <span class="variable">$(BUILD_DIR)</span></span><br><span class="line">	<span class="variable">$(BIN)</span> <span class="variable">$&lt;</span> <span class="variable">$@</span>	</span><br><span class="line">	</span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>:</span><br><span class="line">	mkdir <span class="variable">$@</span></span><br></pre></td></tr></table></figure>

<p>这一部分就是最核心的东西啦，将所有的源文件编译成.o文件，然后再链接到一起，最终生成我们的目标文件。</p>
<h2 id="11-clean-up"><a href="#11-clean-up" class="headerlink" title="11. clean up"></a><font size=3>11. clean up</font></h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># clean up</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	-rm -fR <span class="variable">$(BUILD_DIR)</span></span><br></pre></td></tr></table></figure>

<p>这一部分主要是清理生成的中间文件以及目标文件。</p>
<h2 id="12-dependencies"><a href="#12-dependencies" class="headerlink" title="12. dependencies"></a><font size=3>12. dependencies</font></h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="comment"># dependencies</span></span><br><span class="line"><span class="comment">#######################################</span></span><br><span class="line"><span class="keyword">-include</span> <span class="variable">$(<span class="built_in">wildcard</span> <span class="variable">$(BUILD_DIR)</span>/*.d)</span></span><br></pre></td></tr></table></figure>

<p>依赖引用，这里引用就是前面我们生成的关联文件，这些东西有什么用呢，其实目的就是为了生成我们的.o文件。如果看不懂这个东西什么意思，那么我们随便打开一个d文件其实就应该就懂了：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">build/gpio.o: Core/Src/gpio.c Core/Inc/gpio.h Core/Inc/main.h \</span><br><span class="line"> Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal.h \</span><br><span class="line"> Core/Inc/stm32f1xx_hal_conf.h \</span><br><span class="line"> Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_rcc.h \</span><br><span class="line"> Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_def.h \</span><br><span class="line"> 后边的部分省略......</span><br></pre></td></tr></table></figure>

<p>这个是gpio.d，看着是不是很熟悉，感觉和前面那个《(GCC)STM32CubeMX生成的Makefile详解》说的把生成指令展开后C_INCLUDES、C_SOURCES生成的那一套很像？像就对了，这东西就是为了生成.d文件最顶部那个目标文件而存在的，而这里的gpio.d文件中所指的其实就是gpio.o，其余同理。</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/4e3a140c.html">LV16-07-开发工具-03-Makefile文件</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年06月04日 - 22:06</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/4e3a140c.html" title="LV16-07-开发工具-03-Makefile文件">https://sumumm.github.io/post/4e3a140c.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV16-STM32%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV16-STM32开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/ebcf2720.html" rel="prev" title="LV16-07-开发工具-04-ld链接文件">
                  <i class="fa fa-angle-left"></i> LV16-07-开发工具-04-ld链接文件
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/eabbf614.html" rel="next" title="LV16-07-开发工具-02-STM32CubeIDE">
                  LV16-07-开发工具-02-STM32CubeIDE <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
